/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yiqi.news.app.utils

import android.app.Application
import android.text.TextUtils
import com.jess.arms.mvp.IView
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.utils.RxLifecycleUtils
import com.trello.rxlifecycle2.LifecycleTransformer
import com.trello.rxlifecycle2.RxLifecycle
import com.yiqi.news.app.service.UserController
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.mvp.ui.activity.CodeActivity
import com.yiqi.news.mvp.ui.activity.LoginActivity

import io.reactivex.Observable
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.annotations.NonNull
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * ================================================
 * 放置便于使用 RxJava 的一些工具方法
 *
 *
 * Created by JessYan on 11/10/2016 16:39
 * [Contact me](mailto:jess.yan.effort@gmail.com)
 * [FollowSchool me](https://github.com/JessYanCoding)
 * ================================================
 */
object RxUtils {

    fun <T> applySchedulers(view: IView): ObservableTransformer<T, T> {
        return ObservableTransformer { observable ->
            observable.subscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(RxLifecycleUtils.bindToLifecycle(view))
                    .filter { result -> doCheck(result as BaseResponse<*>) }
        }
    }

    private fun doCheck(response: BaseResponse<*>): Boolean {

        var application: Application? = null

        val code = response.code

//        if (!TextUtils.isEmpty(response.msg)) {
//            ArmsUtils.makeText(application!!.applicationContext, response.msg)
//        }

        if (code == 503) {//token失效
            UserController.getInstance().loginOut()
            ArmsUtils.killAll()
            ArmsUtils.startActivity(LoginActivity::class.java)
            return false
        }else if (code == 1001) {//新用户
            ArmsUtils.startActivity(CodeActivity::class.java)
            return false
        }

        return code == 0
    }


    /**
     * 此方法已废弃
     *
     * @param view
     * @param <T>
     * @return
     * @see RxLifecycleUtils 此类可以实现 {@link RxLifecycle} 的所有功能, 使用方法和之前一致
     *
    </T> */
    @Deprecated("Use {@link RxLifecycleUtils#bindToLifecycle(IView)} instead")
    fun <T> bindToLifecycle(view: IView): LifecycleTransformer<T> {
        return RxLifecycleUtils.bindToLifecycle(view)
    }

}
